package k.daniel.util.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by daniel on 2015/9/14.
 */
public class FileUtil {

    /**
     * 用来copy文件的玩意
     */
    public static class Copy {
        public static void copy(File fromFile, File toFile) throws IOException {
            if (fromFile.exists()) {
                InputStream inputStream = new FileInputStream(fromFile);
                if (!toFile.exists()) {
                    toFile.getParentFile().mkdirs();
                    toFile.createNewFile();

                }
                OutputStream outputStream = new FileOutputStream(toFile);
                byte buffer[] = new byte[4096];
                int count;
                while ((count = inputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, count);
                }
                outputStream.flush();
                outputStream.close();
                inputStream.close();
            }
        }

        public static boolean copy(InputStream inputStream, OutputStream outputStream) throws IOException {
            byte buffer[] = new byte[4096];
            int count;
            while ((count = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, count);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
            return true;
        }
    }
}
